<?php
/**
 * 匠言知识付费
 * Author: 山西匠言网络科技有限公司
 * This is not a free software, it under the license terms, you can visit https://www.zsffzxkc.cn/ get more details.
 */
namespace app\admin\controller;
use think\Db;
use org\Qiniu;
use think\Controller;
use JyAuth\CheckLicense;
//课程管理
class Course extends Controller
{
    public function kaibo_notice(){
        if(request()->isAjax()) {
            ini_set('max_execution_time',"90000000");
            $id = input('param.id');
            $set = $this->get_set('BASE');
            $access_token = get_access_token('H5');
            $access_token=json_decode($access_token,true);
            $get_ticket_url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" . $access_token['access_token'] . "&next_openid=";
            $users=http_curl($get_ticket_url,'','');
            $users=json_decode($users,true);

            $users=$users['data']['openid'];
            $kc = db('coursemenu')->where(['acid'=>1,'id'=>$id])->find();
            $today_zero = strtotime(date("Y-m-d"),time());
            $end = $today_zero+60*60*21;
            $msg = array(
                'first' => array(
                    'value' => "课程即将开始，请您准备好开始学习",
                    'color' => '#ff510'
                ),
                'keyword1' => array(
                    'value' => $kc['menuname'],
                    'color' => '#ff510'
                ),
                'keyword2' => array(
                    'value' => date('Y-m-d H:i',$end),
                    'color' => '#ff510'
                ),
                'remark' => array(
                    'value' => '点击查看详情',
                    'color' => '#ff510'
                ),
            );

            $h5_set = $this->get_set('H5');
            $url = $h5_set['url'].'/pages/course-details/course-details?menuid='.$id;
            if(file_exists(ROOT_PATH.'sendok1.txt')){
                unlink(ROOT_PATH.'sendok1.txt');
            }else{
                file_put_contents(ROOT_PATH.'sendok1.txt','');
            }

            $usernum = 0;
            foreach ($users as $k => $v) {
                $res = $this->sendTpl($v, $set['msg_daka'], $msg, $url);
                if($res){
                    if ($res['errcode'] == 0) {
                        $myfile = fopen(ROOT_PATH.'sendok1.txt', "a+");
                        $txt = $v. '->发送成功' . "\r\n";
                        fwrite($myfile, $txt);
                        fclose($myfile);
                        $usernum++;
                        echo '已提醒'.$usernum.'用户';
                    }else{
                        $myfile = fopen(ROOT_PATH.'sendok1.txt', "a+");
                        $txt = $v. '->发送失败' .$res['errmsg']. "\r\n";
                        fwrite($myfile, $txt);
                        fclose($myfile);
                    }
                    continue;
                }else{
                    break;
                }
            }
            if($res){
                $this->result(0,'提醒发送成功');
            }else{
                $this->result(1,'提醒发送失败');
            }
        }
    }
    public function add_public_courses()
    {
       $post = input('post.');
       $ids = json_decode($post['ids'],true);
       foreach ($ids as $k=>$v){
           $res = db('coursemenu')->where(['acid'=>1,'id'=>$v])->setField('is_public',1);
       }
       if($res){
            $this->result(0,'添加成功');
       }else{
            $this->result(1,'添加失败');
       }
    }

    public function files_del()
    {
        $id = input('id');
        $map = array(
            'acid' => 1,
            'id'=>$id
        );
        $res = db('ziyuan')->where($map)->delete();
        if($res){
            $this->result(0,'删除成功');
        }else{
            $this->result(1,'删除失败');
        }
    }

    public function files_list(){
        $menuid=input('param.menuid');
        $this->assign('menuid',$menuid);
        if(request()->isAjax()) {
            $map = array(
                'a.acid' => 1
            );
            $key = input('keyword');
            $type = input('type');
            if (isset($key) && $key != "") {
                $map['a.name'] = ['like', "%" . $key . "%"];
            }
            if (isset($type) && $type != "") {
                $map['a.type'] = $type;
            }
            $menuid = input('param.menuid');
            $map['menuid']=$menuid;
            $Nowpage = input('get.page') ? input('get.page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $order = input('order');//排序方式

            $data = Db::name('ziyuan')
                ->alias('a')
                ->join('coursemenu b', 'a.menuid=b.id')
                ->field(['a.*', 'b.menuname'])
                ->where($map)
                ->page($Nowpage, $limits)
                ->order($order)
                ->select();
            foreach ($data as $k=>$v) {
                if ($v['type'] == '1') {
                    $media = '图片';
                } elseif ($v['type'] == '2') {
                    $media = '表格';
                } elseif ($v['type'] == '3') {
                    $media = 'PDF';
                }elseif ($v['type'] == '4') {
                    $media = 'Word文档';
                }elseif ($v['type'] == '5') {
                    $media = 'PPT';
                }elseif ($v['type'] == '0') {
                    $media = '未知';
                }
                $data[$k]['type']=$media;
                if ($v['upload_area'] == '0') {
                    $data[$k]['upload_area']='本地';
                }else if ($v['upload_area'] == '1') {
                    $data[$k]['upload_area']='七牛';
                }else if ($v['upload_area'] == '2') {
                    $data[$k]['upload_area']='阿里';
                }else if ($v['upload_area'] == '3') {
                    $data[$k]['upload_area']='腾讯';
                }
                $data[$k]['addtime']=date('Y-m-d H:i:s',$v['addtime']);
                $data[$k]['path']=to_files_path($v['path'],$v['upload_area']);
            }
            $this->result(0,'成功',$data);
        }
        return $this->fetch();
    }
    /*排序修改*/
    public function course_edit(){
        if(request()->isAjax()) {
            //编号
            $id = input('param.id');
            //修改字段
            $field = input('param.field');
            //值
            $value = input('param.value');
            $db=input('param.db');
            $map=['id'=>$id,'acid'=>1];
            $res=db($db)->where($map)->update([$field=>$value]);
            if($res){
                writelog('更新字段成功',200);
                return json(['code' => 200,'data' => '', 'msg' => '操作成功']);
            }else{
                writelog('更新字段失败',100);
                return json(['code' => 100,'data' => '', 'msg' => '操作失败']);
            }
        }
    }
    //课程有效期设置
    public function yxq_set(){
        $menuid=input('param.menuid');
        $map=['id'=>$menuid,'acid'=>1];
        if(isset($menuid)){
            $menuinfo=Db::name('coursemenu')
                ->where($map)
                ->find();
            if(isset($media)){
                $menuinfo['media']=$media;
            }
            $this->assign('menuinfo',$menuinfo);
        }else{
            $this->assign('menuinfo',array());
        }
        $yxqs = db('course_yxq')->where(['acid'=>1])->select();
        if(!empty($yxqs)){
            $this->assign('yxqs',$yxqs);
        }else{
            $this->assign('yxqs',array());
        }
        if(request()->isPost()){
            if(session('agid')=='2'){
                $this->result('1','暂无权限');
            }else {
                $data = input('post.');
                $id = $data['id'];
                foreach ($data as $k=>$v){
                    if($v=='on'){
                        $data[$k]=1;
                    }elseif ($v=='off'){
                        $data[$k]=0;
                    }
                }
                $data1['is_yxq'] = $data['is_yxq'];
                unset($data['is_yxq']);
                unset($data['id']);
                $data1['yxqdata'] = json_encode($data);
                if($id!=''){
                    $res=Db::name('coursemenu')->where('id',$id)->update($data1);
                    if($res){
                        if($res==0){
                            $this->result('0','无更新');
                        }else{
                            $this->result('0','设置成功');
                        }
                    }else{
                        $this->result('1','设置失败');
                    }
                }
            }
        }
        return $this->fetch();
    }
    //课程有效期
    public function timepart_del(){
        if(request()->isAjax()) {
            $id = input('param.id');
            $res=Db::name('course_yxq')->where('acid',1)->where('id',$id)->delete();
            if($res){
                $this->result(0,'删除成功');
            }else{
                $this->result(1,'删除失败');
            }
        }
    }
    public function timepart(){
        if(request()->isAjax()) {
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $data = Db::name('course_yxq')
                ->where('acid',1)
                ->page($Nowpage, $limits)
                ->order($order)
                ->select();
            $total=Db::name('course_yxq')->where('acid',1)->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function timepartadd(){
        $id=input('param.id');
        if(isset($id)){
            $item=Db::name('course_yxq')
                ->where('id',$id)
                ->where('acid',1)
                ->find();
            if(!empty($item)){
                $this->assign('item',$item);
            }
        }else{
            $this->assign('item',array());
        }
        if(request()->isPost()){
            $data = input('post.');
            if($data['id']!=''){
                $res=Db::name('course_yxq')->where('acid',1)->where('id',$data['id'])->update($data);
                if($res){
                    if($res==0){
                        $this->result('0','无更新');
                    }else{
                        $this->result('0','修改成功');
                    }
                }else{
                    $this->result('1','修改失败');
                }
            }else{
                $data['acid']=1;
                $res=Db::name('course_yxq')->insert($data);
                if($res){
                    $this->result('0','添加成功');
                }else{
                    $this->result('1','添加失败');
                }
            }
        }
        return $this->fetch();
    }
    public function status_switch(){
        $id = input('param.id');
        $type=input('param.type');
        if($type=='menu'){
            $table=Db::name('coursemenu');
        }else{
            $table=Db::name('course');
        }
        $status = input('param.status');
        $data=array(
            'status'=>$status
        );
        $res=$table->where('id',$id)->update($data);
        if($res){
            $this->result(200,'操作成功');
        }else{
            $this->result(500,'操作失败');
        }
    }
    public function menu_sh(){
        $id = input('param.id');
        $type=input('param.type');
        if(session('agid')=='6'){
            $this->result(500,'您无权操作');
        }
        if($type=='menu'){
            $table=Db::name('coursemenu');
        }else{
            $table=Db::name('course');
        }
        $data=array(
            'is_verify'=>1
        );
        $res=$table->where('id',$id)->update($data);
        if($res){
            $this->result(200,'操作成功');
        }else{
            $this->result(500,'操作失败');
        }
    }
    public function yxq_switch(){
        $id = input('param.id');
        $type=input('param.type');

        if($type=='menu'){
            $table=Db::name('coursemenu');
        }
        $status = input('param.status');
        $data=array(
            'is_yxq'=>$status
        );
        $menuinfo = $table->where('id',$id)->find();
        if($status==1 && $menuinfo['one_lock']==1){
            $this->result(500,'设置单课程解锁与设置有效期只能二选一，请重试');
        }
        $res=$table->where('id',$id)->update($data);
        if($res){
            $this->result(200,'操作成功');
        }else{
            $this->result(500,'操作失败');
        }
    }
    public function tj_switch(){
        $id = input('param.id');
        $type=input('param.type');
        if($type=='menu'){
            $table=Db::name('coursemenu');
        }else{
            $table=Db::name('course');
        }
        $status = input('param.status');
        $data=array(
            'is_tj'=>$status
        );
        $res=$table->where('id',$id)->update($data);
        if($res){
            $this->result(200,'操作成功');
        }else{
            $this->result(500,'操作失败');
        }
    }
    public function dsh(){
        $media=input('param.media');
        $this->assign('media',$media);
        if(request()->isAjax()) {
            $map=array(
                'a.acid'=>1,
                'a.is_verify'=>0
            );
            $key=input('keyword');

            if(isset($key)&&$key!=""){
                $map['a.menuname'] = ['like',"%" . $key . "%"];
            }
            if(isset($media)&&$media!=""){
                $map['a.media'] = $media;
            }
            if(session('agid')=='6'){
                $admininfo=db('admin')->where('id',session('uid'))->find();
                if(!empty($admininfo)){
                    $map['a.tid']=$admininfo['tid'];
                }
            }
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $admininfo=db('admin')->where('id',session('uid'))->find();
            if(session('agid')!=1){
                $map['a.jgid']=$admininfo['jigouid'];
            }
            $data = Db::name('coursemenu')
                ->alias('a')
                ->join('teacher b','a.tid=b.id')
                ->field(['a.*','b.imgname'=>'teacher'])
                ->where('a.status','in','1,0')
                ->where($map)
                ->page($Nowpage, $limits)
                ->order($order)
                ->select();
            foreach ($data as $k=>$v){
                if($v['media']=='video'){
                    $media='视频';
                }elseif($v['media']=='audio'){
                    $media='音频';
                }elseif($v['media']=='tuwen'){
                    $media='图文';
                }elseif($v['media']=='all'){
                    $media='不限';
                }
                if($v['lid']!=0) {
                    $levelinfo = db('viplevel')->where(['acid' => 1, 'id' => $v['lid']])->find();
                    $data[$k]['levelname'] = $levelinfo['name'].'及以上';
                }else{
                    $data[$k]['levelname'] = '不限';
                }
                $ksnum=db('course')->where(['acid'=>1,'status'=>1,'menuid'=>$v['id']])->count();
                $data[$k]['ksnum']=$ksnum;
                $classifyname='';
                if(strpos($v['fenleiid'],',')){
                    $fls=explode(",",$v['fenleiid']);
                    foreach ($fls as $key=>$val){
                        $title=Db::name('classify')
                            ->field('title')
                            ->where('id',$val)
                            ->find();
                        $classifyname.=($title['title'].' | ');
                    }
                    $classifyname=rtrim($classifyname,' |');
                }else{
                    $title=Db::name('classify')
                        ->field('title')
                        ->where('id',$v['fenleiid'])
                        ->find();
                    $classifyname=$title['title'];
                }
                $data[$k]['media_name']=$media;
                $data[$k]['classify']=$classifyname;
                $data[$k]['thumb']= to_media_url($data[$k]['thumb']);
            }
            $total=Db::name('coursemenu')->alias('a')->where($map)->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function deleted_menus(){
        $media=input('param.media');
        $this->assign('media',$media);
        if(request()->isAjax()) {
            $map=array(
                'a.acid'=>1,
                'a.is_verify'=>1
            );
            $key=input('keyword');

            if(isset($key)&&$key!=""){
                $map['a.menuname'] = ['like',"%" . $key . "%"];
            }
            if(isset($media)&&$media!=""){
                $map['a.media'] = $media;
            }
            if(session('agid')=='6'){
                $admininfo=db('admin')->where('id',session('uid'))->find();
                if(!empty($admininfo)){
                    $map['a.tid']=$admininfo['tid'];
                }
            }
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $admininfo=db('admin')->where('id',session('uid'))->find();
            if(session('agid')!=1){
                $map['a.jgid']=$admininfo['jigouid'];
            }
            $data = Db::name('coursemenu')
                ->alias('a')
                ->join('teacher b','a.tid=b.id')
                ->field(['a.*','b.imgname'=>'teacher'])
                ->where('a.status','2')
                ->where($map)
                ->page($Nowpage, $limits)
                ->order($order)
                ->select();
            foreach ($data as $k=>$v){
                if($v['media']=='video'){
                    $media='视频';
                }elseif($v['media']=='audio'){
                    $media='音频';
                }elseif($v['media']=='tuwen'){
                    $media='图文';
                }elseif($v['media']=='all'){
                    $media='不限';
                }
                if($v['lid']!=0) {
                    $levelinfo = db('viplevel')->where(['acid' => 1, 'id' => $v['lid']])->find();
                    $data[$k]['levelname'] = $levelinfo['name'].'及以上';
                }else{
                    $data[$k]['levelname'] = '不限';
                }
                $ksnum=db('course')->where(['acid'=>1,'status'=>1,'menuid'=>$v['id']])->count();
                $data[$k]['ksnum']=$ksnum;
                $classifyname='';
                if(strpos($v['fenleiid'],',')){
                    $fls=explode(",",$v['fenleiid']);
                    foreach ($fls as $key=>$val){
                        $title=Db::name('classify')
                            ->field('title')
                            ->where('id',$val)
                            ->find();
                        $classifyname.=($title['title'].' | ');
                    }
                    $classifyname=rtrim($classifyname,' |');
                }else{
                    $title=Db::name('classify')
                        ->field('title')
                        ->where('id',$v['fenleiid'])
                        ->find();
                    $classifyname=$title['title'];
                }
                $data[$k]['media_name']=$media;
                $data[$k]['classify']=$classifyname;
                $data[$k]['thumb']= to_media_url($data[$k]['thumb']);
            }
            $total=Db::name('coursemenu')->alias('a')->where($map)->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function index(){
        $media=input('param.media');
        $this->assign('media',$media);
        if(request()->isAjax()) {
            $map=array(
                'a.acid'=>1,
                'a.is_verify'=>1
            );
            $key=input('keyword');

            if(isset($key)&&$key!=""){
                $map['a.menuname'] = ['like',"%" . $key . "%"];
            }
            if(isset($media)&&$media!=""){
                $map['a.media'] = $media;
            }
            if(session('agid')=='6'){
                $admininfo=db('admin')->where('id',session('uid'))->find();
                if(!empty($admininfo)){
                    $map['a.tid']=$admininfo['tid'];
                }
            }
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $order=input('order');//排序方式
            $admininfo=db('admin')->where('id',session('uid'))->find();
            if(session('agid')!=1){
                $map['a.jgid']=$admininfo['jigouid'];
            }
            $data = Db::name('coursemenu')
                ->alias('a')
                ->join('teacher b','a.tid=b.id')
                ->field(['a.*','b.imgname'=>'teacher'])
                ->where('a.status','in','1,0')
                ->where($map)
                ->page($Nowpage, $limits)
                ->order($order)
                ->select();
            foreach ($data as $k=>$v){
                if($v['media']=='video'){
                    $media='视频';
                }elseif($v['media']=='audio'){
                    $media='音频';
                }elseif($v['media']=='tuwen'){
                    $media='图文';
                }elseif($v['media']=='all'){
                    $media='不限';
                }
                if($v['lid']!=0) {
                    $levelinfo = db('viplevel')->where(['acid' => 1, 'id' => $v['lid']])->find();
                    $data[$k]['levelname'] = $levelinfo['name'].'及以上';
                }else{
                    $data[$k]['levelname'] = '不限';
                }
                $ksnum=db('course')->where(['acid'=>1,'status'=>1,'menuid'=>$v['id']])->count();
                $data[$k]['ksnum']=$ksnum;
                $classifyname='';
                if(strpos($v['fenleiid'],',')){
                    $fls=explode(",",$v['fenleiid']);
                    foreach ($fls as $key=>$val){
                        $title=Db::name('classify')
                            ->field('title')
                            ->where('id',$val)
                            ->find();
                        $classifyname.=($title['title'].' | ');
                    }
                    $classifyname=rtrim($classifyname,' |');
                }else{
                    $title=Db::name('classify')
                        ->field('title')
                        ->where('id',$v['fenleiid'])
                        ->find();
                    $classifyname=$title['title'];
                }
                $data[$k]['media_name']=$media;
                $data[$k]['classify']=$classifyname;
                $data[$k]['thumb']= to_media_url($data[$k]['thumb']);
            }
            $total=Db::name('coursemenu')->alias('a')->where($map)->count();
            $this->result(0,'成功',$data,$total);
        }
        return $this->fetch();
    }
    public function course_list(){
        $menuid=input('param.menuid');
        $this->assign('menuid',$menuid);
        if(request()->isAjax()) {
            $map = array(
                'a.acid' => 1
            );
            $key = input('keyword');

            if (isset($key) && $key != "") {
                $map['a.coursename'] = ['like', "%" . $key . "%"];
            }
            if (isset($media) && $media != "") {
                $map['a.media'] = $media;
            }
            $menuid = input('param.menuid');
            $map['menuid']=$menuid;
            $Nowpage = input('get.page') ? input('get.page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $order = input('order');//排序方式

            $data = Db::name('course')
                ->alias('a')
                ->join('coursemenu b', 'a.menuid=b.id')
                ->field(['a.*', 'b.menuname'])
                ->where('a.status', 'in', '1,0')
                ->where($map)
                ->page($Nowpage, $limits)
                ->order($order)
                ->select();
            foreach ($data as $k=>$v) {
                if ($v['media'] == 'video') {
                    $media = '视频';
                } elseif ($v['media'] == 'audio') {
                    $media = '音频';
                } elseif ($v['media'] == 'tuwen') {
                    $media = '图文';
                }
                $data[$k]['media_name']=$media;
            }

            //$total=Db::name('course')->count();
            $this->result(0,'成功',$data);
        }
        return $this->fetch();
    }
    public function huifu(){
        if(request()->isAjax()) {
            if(session('agid')!='1'){
                $this->result(1,'无权限');
            }
            $db = input('param.db');
            $id = input('param.id');
            $map=['id'=>$id,'acid'=>1];
            $res=db($db)->where($map)->update(['status'=>0]);
            if($res){
                $this->result(0,'恢复成功');
            }else{
                $this->result(1,'恢复失败');
            }
        }
    }
    public function real_del(){
        if(request()->isAjax()) {
            if(session('agid')!='1'){
                $this->result(1,'无权限');
            }
            $db = input('param.db');
            $id = input('param.id');
            $map=['id'=>$id,'acid'=>1];
            $res=db($db)->where($map)->delete();
            if($res){
                $this->result(0,'删除成功');
            }else{
                $this->result(1,'删除失败');
            }
        }
    }
    public function menu_del(){
        if(request()->isAjax()) {
            $id = input('param.id');
            $map=['id'=>$id,'acid'=>1];
            $menuinfo=Db::name('coursemenu')->where($map)->find();
            if(session('agid')=='6'){
                $admininfo=db('admin')->where('id',session('uid'))->find();
                if(!empty($admininfo)){
                    $tid=$admininfo['tid'];
                    if($menuinfo['tid']!=$tid){
                        return $this->error('禁止越权删除！');
                    }
                }
            }
            $res=Db::name('coursemenu')->where($map)->update(['status'=>2]);
            if($res){
                $this->result(0,'删除成功');
            }else{
                $this->result(1,'删除失败');
            }
        }
    }
    public function course_del(){
        if(request()->isAjax()) {
            $id = input('param.id');
            $map=['id'=>$id,'acid'=>1];
            $courseinfo=Db::name('course')->where($map)->find();
            $menuinfo=Db::name('coursemenu')->where(['id'=>$courseinfo['menuid']])->find();

            if(session('agid')=='6'){
                $admininfo=db('admin')->where('id',session('uid'))->find();
                if(!empty($admininfo)){
                    $tid=$admininfo['tid'];
                    if($menuinfo['tid']!=$tid){
                        return $this->error('禁止越权删除！');
                    }
                }
            }
            $res=Db::name('course')->where($map)->update(['status'=>2]);
            if($res){
                $this->result(0,'删除成功');
            }else{
                $this->result(1,'删除失败');
            }
        }
    }
    public function add_menu(){
        $id=input('param.id');
        $map=['id'=>$id,'acid'=>1];
        if(isset($id)){
            $item=Db::name('coursemenu')
                ->where($map)
                ->find();
            if(!empty($item)){
                $this->assign('item',$item);
            }
        }else{
            $this->assign('item',array(
                'one_lock'=>'',
                'is_sw'=>'',
                'status'=>'',
                'is_fx'=>'',
                'is_yxq'=>''
            ));
        }
        if(request()->isPost()){
            if(session('agid')=='2'){
                $this->result('1','暂无权限');
            }else{
                $admininfo=db('admin')->where('id',session('uid'))->find();
                $data = input('post.');
                $data['jgid']=$admininfo['jigouid'];
                unset($data['file']);
                if($data['id']!=''){
                    $res=Db::name('coursemenu')->where('id',$data['id'])->update($data);
                    if($res){
                        if($res==0){
                            $this->result('0','无更新');
                        }else{
                            $this->result('0','修改成功');
                        }
                    }else{
                        $this->result('1','修改失败');
                    }
                }else{
                    if(session('agid')!='1'){
                        $data['is_verify']=0;
                    }else{
                        $data['is_verify']=1;
                    }
                    $data['acid']=1;
                    $data['addtime']=time();
                    $res=Db::name('coursemenu')->insert($data);
                    if($res){
                        $this->result('0','添加成功');
                    }else{
                        $this->result('1','添加失败');
                    }
                }
            }
        }
        return $this->fetch();
    }
//    添加课件
    public function add_files(){
        $menuid=input('param.menuid');
        $map=['id'=>$menuid,'acid'=>1];
        $set=Db::name('storage')->where('acid',1)->find();
        $this->assign('upload_area',$set['upload_area']);
        if(isset($menuid)){
            $menuinfo=Db::name('coursemenu')
                ->where($map)
                ->find();
            $this->assign('menuinfo',$menuinfo);
        }else{
            $this->assign('menuinfo',array());
        }
        $menus=Db::name('coursemenu')
            ->where('acid',1)
            ->select();
        $this->assign('menus',$menus);
        $id=input('param.id');
        if(request()->isPost()){
            if(session('agid')=='2'){
                $this->result('1','暂无权限');
            }else {
                $data = input('post.');
                unset($data['file']);
                if ($data['id'] != '') {
                    $res = Db::name('ziyuan')->where('id', $data['id'])->update($data);
                    if ($res) {
                        if ($res == 0) {
                            $this->result('0', '无更新');
                        } else {
                            $this->result('0', '修改成功');
                        }
                    } else {
                        $this->result('1', '修改失败');
                    }
                } else {
                    $data['addtime'] = time();
                    $data['acid'] = 1;
                    $data['media'] = 'course';
                    $res = Db::name('ziyuan')->insert($data);
                    if ($res) {
                        $this->result('0', '添加成功');
                    } else {
                        $this->result('1', '添加失败');
                    }
                }
            }
        }
        return $this->fetch();
    }
    public function add_course(){
        $menuid=input('param.menuid');
        $media=input('param.media');
        $map=['id'=>$menuid,'acid'=>1];
        $set=Db::name('storage')->where('acid',1)->find();
        $this->assign('upload_area',$set['upload_area']);
        $this->assign('qiniu',json_decode($set['qiniu'],true));
        if(isset($menuid)){
            $menuinfo=Db::name('coursemenu')
                ->where($map)
                ->find();
            if(isset($media)){
                $menuinfo['media']=$media;
            }
            $this->assign('menuinfo',$menuinfo);
        }else{
            $this->assign('menuinfo',array());
        }
        $menus=Db::name('coursemenu')
            ->where('acid',1)
            ->select();
        $medias=array(
            ['medianame'=>'视频','val'=>'video'],
            ['medianame'=>'音频','val'=>'audio'],
            ['medianame'=>'图文','val'=>'tuwen'],
        );
        $this->assign('menus',$menus);
        $this->assign('medias',$medias);
        $id=input('param.id');
        $map1=['id'=>$id,'acid'=>1];
        if(isset($id)){
            $item=Db::name('course')
                ->where($map1)
                ->find();
            if(!empty($item)){
                $menuinfo=Db::name('coursemenu')
                    ->where(['acid'=>1,'id'=>$item['menuid']])
                    ->find();
                $this->assign('menuinfo',$menuinfo);
                $this->assign('item',$item);
            }
        }else{
            $this->assign('item',array());
        }
        if(request()->isPost()){
            if(session('agid')=='2'){
                $this->result('1','暂无权限');
            }else {
                $data = input('post.');
                unset($data['file']);
                $menumedia = $data['menumedia'];
                $set=Db::name('storage')->where('acid',1)->find();
                $data1 = array(
                    'menuid' => $data['menuid'],
                    'acid' => 1,
                    'coursename' => $data['coursename'],
                    'freesecond' => $data['freesecond']?$data['freesecond']:0,
                    'addtime' => time(),
                    'price' => $data['price'],
                    'is_sk'=> $data['is_sk'],
                    'upload_area' => $set['upload_area']
                );
                switch ($menumedia) {
                    case 'video':
                        $data1['src'] = $data['video_src']?$data['video_src']:$data['video_src1'];
                        $data1['media'] = $menumedia;
                        break;
                    case 'audio':
                        $data1['src'] = $data['audio_src'];
                        $data1['media'] = $menumedia;
                        break;
                    case 'tuwen':
                        $data1['media'] = $menumedia;
                        $data1['introduce'] = $data['introduce'];
                        break;
                }
                if ($data['id'] != '') {
                    $res = Db::name('course')->where('id', $data['id'])->update($data1);
                    if ($res) {
                        if ($res == 0) {
                            $this->result('0', '无更新');
                        } else {
                            $this->result('0', '修改成功');
                        }
                    } else {
                        $this->result('1', '修改失败');
                    }
                } else {
                    $res = Db::name('course')->insert($data1);
                    if ($res) {
                        $this->result('0', '添加成功');
                    } else {
                        $this->result('1', '添加失败');
                    }
                }
            }
        }
        return $this->fetch();
    }
}
